<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="tasksName">
        <el-input
          v-model="queryParams.tasksName"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务周数" prop="tasksWeek">
        <el-input
          v-model="queryParams.tasksWeek"
          placeholder="请输入任务周时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="完成状态" prop="tasksType">
        <el-select
          v-model="queryParams.tasksType"
          placeholder="完成状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleAdd"
          v-hasPermi="['fuw:tasks:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fuw:tasks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fuw:tasks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['fuw:tasks:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tasksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务编号" align="center" prop="tasksId" :show-overflow-tooltip='true'/>
      <el-table-column label="任务名称" align="center" prop="tasksName" :show-overflow-tooltip='true'/>
      <el-table-column label="规定开始时间" align="center" prop="tasksStartTime" width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tasksStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="规定完成时间" align="center" prop="tasksEndTime" width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tasksEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务周时间" align="center" prop="tasksWeek" width="100"/>
      <el-table-column label="完成状态" align="center" prop="tasksType" />
      <el-table-column label="任务权重" align="center" prop="tasksNum" />
      <el-table-column label="实际完成时间" align="center" prop="tasksOutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tasksOutTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="医务人员" align="center" prop="medicalId" />
      <el-table-column label="组别编号" align="center" prop="workId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetails(scope.row)"
            v-hasPermi="['fuw:tasks:details']"
          >详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['fuw:tasks:edit']"-->
<!--          >详情</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fuw:tasks:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 任务详情对话框 -->
    <el-dialog :title="title" :visible.sync="openNow" width="880px" append-to-body>
      <el-descriptions class="margin-top" title="" :column="4" border>
        <el-descriptions-item :span="4">
          <template slot="label">
            <i class="el-icon-user"></i>
            任务编号
          </template>
          {{form.tasksId}}
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            任务名称
          </template>
          {{form.tasksName}}
        </el-descriptions-item>
        <el-descriptions-item v-if="form.medicalId != ''" :span="2">
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            医务人员
          </template>
          {{form.medicalId}}
        </el-descriptions-item>
        <el-descriptions-item v-else :span="2">
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            组别人员
          </template>
          {{form.workId}}
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            规定开始时间
          </template>
          {{form.tasksStartTime}}
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            规定完成时间
          </template>
          {{form.tasksEndTime}}
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            实际完成时间
          </template>
          <span v-if="form.tasksOutTime != null">{{form.tasksOutTime}}</span>
          <span v-else>未完成</span>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            完成状态
          </template>
          <!-- （0：任务创建；1：任务进行中；2：任务逾期；3：任务完成） -->
          <el-tag size="small" v-if="form.tasksType == 0">任务创建</el-tag>
          <el-tag size="small" v-else-if="form.tasksType == 1">任务进行中</el-tag>
          <el-tag size="small" type="warning" v-else-if="form.tasksType == 2">任务逾期</el-tag>
          <el-tag size="small" type="success" v-else-if="form.tasksType == 3">任务完成</el-tag>
          <el-tag size="small" type="danger" v-else>未识别</el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            任务周时间
          </template>
          {{form.tasksWeek}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            任务权重
          </template>
          <div class="block">
            <el-slider
              v-model="form.tasksNum"
              :step="1"
              :max="9"
              disabled
              show-stops>
            </el-slider>
          </div>
        </el-descriptions-item>


      </el-descriptions>
    </el-dialog>

  </div>
</template>

<script>
import { listTasks, getTasks, delTasks, addTasks, updateTasks, exportTasks } from "@/api/fuw/tasks";

export default {
  name: "Tasks",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务记录表格数据
      tasksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openNow: false,
      // 任务状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tasksName: null,
        tasksStartTime: null,
        tasksEndTime: null,
        tasksWeek: null,
        tasksType: null,
        tasksNum: null,
        tasksOutTime: null,
        medicalId: null,
        workId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("fuw_task_type").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询任务记录列表 */
    getList() {
      this.loading = true;
      listTasks(this.queryParams).then(response => {
        this.tasksList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openNow = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tasksId: null,
        tasksName: null,
        tasksStartTime: null,
        tasksEndTime: null,
        tasksWeek: null,
        tasksType: null,
        tasksNum: null,
        tasksOutTime: null,
        medicalId: null,
        workId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tasksId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tasksId = row.tasksId || this.ids
      getTasks(tasksId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务记录";
      });
    },
    /** 详情按钮操作 */
    handleDetails(row) {
      this.reset();
      const tasksId = row.tasksId || this.ids
      getTasks(tasksId).then(response => {
        this.form = response.data;
        this.openNow = true;
        this.title = "任务记录详情";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.tasksId != null) {
            updateTasks(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTasks(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tasksIds = row.tasksId || this.ids;
      this.$confirm('是否确认删除任务记录编号为"' + tasksIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTasks(tasksIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有任务记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportTasks(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
